package io.renren.modules.sys.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.utils.Tools;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.sys.entity.MaterialcategoryEntity;
import io.renren.modules.sys.service.MaterialcategoryService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 产品类型表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-11-22 14:29:36
 */
@RestController
@RequestMapping("sys/materialcategory")
public class MaterialcategoryController {
    @Autowired
    private MaterialcategoryService materialcategoryService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {


        List<MaterialcategoryEntity> materialcategoryEntities = materialcategoryService.selectList(new EntityWrapper<MaterialcategoryEntity>()
                .like("ParentId", Tools.dealNullStr(params.get("ParentId")))
                .ne("id", 1)
                .orderBy("id")
        );


        return R.ok().put("materialCategoryList", materialcategoryEntities);
    }


    @RequestMapping("/findBy")
    public R findBy(@RequestParam Map<String, Object> params) {
        Integer pageSize = 15;
        Integer pageNo = 1;
        Object pageSizeObj = params.get("pageSize");
        Object pageNoObj = params.get("pageNo");
        if(pageSizeObj!=null){
            pageSize=Integer.parseInt(pageSizeObj.toString());
        }
        if(pageNoObj!=null){
            pageNo=Integer.parseInt(pageNoObj.toString());
        }
        params.put("limit",pageSize.toString());
        params.put("page",pageNo.toString());

        PageUtils page = materialcategoryService.queryPage(params);
        List<MaterialcategoryEntity> list = materialcategoryService.selectList(
                new EntityWrapper<MaterialcategoryEntity>()
                        .eq("ParentId", Tools.dealNullStr(params.get("ParentId")))
                        .like("name", Tools.dealNullStr(params.get("Name")))
                        .ne("id",1)
                        .orderBy("id"));

        JSONArray dataArray = new JSONArray();
        if (null != list) {
            for (MaterialcategoryEntity materialCategory : list) {
                JSONObject item = new JSONObject();
                item.put("Id", materialCategory.getId());
                item.put("ParentId", materialcategoryService.selectById(materialCategory.getId()).getId());
                item.put("ParentName", materialcategoryService.selectById(materialCategory.getId()).getName());
                item.put("CategoryLevel", materialCategory.getCategorylevel());
                item.put("Name", materialCategory.getName());
                item.put("op", 1);
                dataArray.add(item);
            }
        }
        return R.ok().put("rows", dataArray).put("total", list.size());
    }

    @RequestMapping("/getBasicData")
    public R getBasicData(@RequestParam Map<String, Object> params) {
        PageUtils page = materialcategoryService.queryPage(params);
        List<MaterialcategoryEntity> list = materialcategoryService.selectList(
                new EntityWrapper<MaterialcategoryEntity>()
                        .eq("ParentId", Tools.dealNullStr(params.get("ParentId")))
                        .ne("id",1)
                        .orderBy("id"));

        return R.ok().put("materialCategoryList", page.getList());
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:materialcategory:info")
    public R info(@PathVariable("id") Long id) {
        MaterialcategoryEntity materialcategory = materialcategoryService.selectById(id);

        return R.ok().put("materialcategory", materialcategory);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:materialcategory:save")
    public R save(@RequestBody MaterialcategoryEntity materialcategory) {
        materialcategoryService.insert(materialcategory);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:materialcategory:update")
    public R update(@RequestBody MaterialcategoryEntity materialcategory) {
        ValidatorUtils.validateEntity(materialcategory);
        materialcategoryService.updateAllColumnById(materialcategory);//全部更新

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:materialcategory:delete")
    public R delete(@RequestBody Long[] ids) {
        materialcategoryService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

}
